package com.atguigu.gmall.cart.config;

import com.atguigu.gmall.common.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.io.File;
import java.security.PrivateKey;
import java.security.PublicKey;

@Slf4j
@Data
@ConfigurationProperties(prefix = "jwt")
public class JwtProperties {

    private String pubKeyPath;
    private String cookieName;
    private String userKey;
    private Integer expire;

    private PublicKey publicKey;

    @PostConstruct
    public void init(){
        try {
            File pubFile = new File(pubKeyPath);
            // 任何一个文件不存在则重新生成
            if (pubFile.exists()){
                // 读取公私钥文件赋值给公私钥对象
                this.publicKey = RsaUtils.getPublicKey(pubKeyPath);
            }
        } catch (Exception e) {
            log.error("读取公钥失败。");
        }
    }
}
